package com.another.bidservice.utils.util.query;


import com.another.bidservice.entity.TColumn;
import com.another.common.utils.common.MyException;
import com.another.common.utils.result.ResultEnum;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.util.List;


public class ParameterProcessing {

    public static String dealParameter(List<QueryParameter> queryParameterList) {
        StringBuffer parameter = new StringBuffer("AND (");
        for (int i = 0; i < queryParameterList.size(); i++) {

            //  parameter.append(" "+queryParameterList.get(i).getParameter());//参数名
            parameter.append(" " + ParameterProcessing.HumpToUnderline(queryParameterList.get(i).getParameter()));//参数名
            switch (queryParameterList.get(i).getQuery()) {
                case "等于":
                    parameter.append(" " + QureyEnums.QueryEnum.getDisplayValue(queryParameterList.get(i).getQuery()));//条件
                    parameter.append(" '" + queryParameterList.get(i).getValue1() + "'"); //值
                    break;
                case "不等于":
                    parameter.append(" " + QureyEnums.QueryEnum.getDisplayValue(queryParameterList.get(i).getQuery()));//条件
                    parameter.append(" '" + queryParameterList.get(i).getValue1() + "'"); //值
                    break;
                case "小于":
                    parameter.append(" " + QureyEnums.QueryEnum.getDisplayValue(queryParameterList.get(i).getQuery()));//条件
                    parameter.append(" '" + queryParameterList.get(i).getValue1() + "'"); //值
                    break;
                case "小于或等于":
                    parameter.append(" " + QureyEnums.QueryEnum.getDisplayValue(queryParameterList.get(i).getQuery()));//条件
                    parameter.append(" '" + queryParameterList.get(i).getValue1() + "'"); //值
                    break;
                case "大于":
                    parameter.append(" " + QureyEnums.QueryEnum.getDisplayValue(queryParameterList.get(i).getQuery()));//条件
                    parameter.append(" '" + queryParameterList.get(i).getValue1() + "'"); //值
                    break;
                case "大于或等于":
                    parameter.append(" " + QureyEnums.QueryEnum.getDisplayValue(queryParameterList.get(i).getQuery()));//条件
                    parameter.append(" '" + queryParameterList.get(i).getValue1() + "'"); //值
                    break;
                case "包含":
                    parameter.append(" " + QureyEnums.QueryEnum.getDisplayValue(queryParameterList.get(i).getQuery()));//条件
                    parameter.append(" CONCAT('%','" + queryParameterList.get(i).getValue1() + "','%')");//值
                    break;
                case "不包含":
                    parameter.append(" " + QureyEnums.QueryEnum.getDisplayValue(queryParameterList.get(i).getQuery()));//条件
                    parameter.append(" ('" + queryParameterList.get(i).getValue1() + "')");//值
                    break;
                case "在列表中":
                    parameter.append(" " + QureyEnums.QueryEnum.getDisplayValue(queryParameterList.get(i).getQuery()));//条件
                    parameter.append("(" + queryParameterList.get(i).getValue1() + ")");//值
                    break;
                case "介于":
                    parameter.append(" " + QureyEnums.QueryEnum.getDisplayValue(queryParameterList.get(i).getQuery()));//条件
                    parameter.append(" '" + queryParameterList.get(i).getValue1() + "'");//值1
                    parameter.append(" AND '" + queryParameterList.get(i).getValue2() + "'");//值2
                    break;

            }
//            parameter.append(" "+Enums.QueryEnum.getDisplayValue(queryParameterList.get(i).getQuery()));//条件
//            parameter.append(" "+queryParameterList.get(i).getValue1());//值
            if (i != queryParameterList.size() - 1) {
                parameter.append(" " + QureyEnums.PostQueryEnum.getDisplayValue(queryParameterList.get(i).getPostQuery()));//后置条件
            }
        }
        parameter.append(" )");
        return parameter.toString();
    }


    public static String spliceColumn(List<TColumn> columns) {
        StringBuffer tableColumns = new StringBuffer();
        for (TColumn column : columns) {
            tableColumns.append(HumpToUnderline(column.getColEname()) + " as " + column.getColEname() + ",");
        }
        String result = tableColumns.toString();
        return result.substring(0, result.length() - 1);
    }

    /**
     * 驼峰命名转为下划线命名
     *
     * @param para
     * @return
     */
    public static String HumpToUnderline(String para) {
        StringBuilder sb = new StringBuilder(para);
        int temp = 0;//定位
        for (int i = 0; i < para.length(); i++) {
            if (Character.isUpperCase(para.charAt(i))) {
                sb.insert(i + temp, "_");
                temp += 1;
            }
        }
        return sb.toString().toUpperCase();
    }

    /**
     * 下划线命名转为驼峰命名
     *
     * @param para
     * @return
     */
    public static String UnderlineToHump(String para) {
        StringBuilder result = new StringBuilder();
        String a[] = para.split("_");
        for (String s : a) {
            if (result.length() == 0) {
                result.append(s.toLowerCase());
            } else {
                result.append(s.substring(0, 1).toUpperCase());
                result.append(s.substring(1).toLowerCase());
            }
        }
        return result.toString();
    }


    /**
     * 属性复制
     *
     * @param source 来源对象
     * @param target 目标对象
     * @throws Exception
     */
    public static void CopyProperty(Object source, Object target) throws Exception {
        BeanInfo sourceBean = Introspector.getBeanInfo(source.getClass(), Object.class);
        PropertyDescriptor[] sourceProperty = sourceBean.getPropertyDescriptors();

        BeanInfo targetBean = Introspector.getBeanInfo(target.getClass(), Object.class);
        PropertyDescriptor[] targetProperty = targetBean.getPropertyDescriptors();

        try {
            for (int i = 0; i < sourceProperty.length; i++) {

                for (int j = 0; j < targetProperty.length; j++) {
                    if (sourceProperty[i].getName().equals(targetProperty[j].getName()) && sourceProperty[i].getPropertyType() == targetProperty[j].getPropertyType()) {
                        if (sourceProperty[i].getReadMethod().invoke(source) != null) {
                            targetProperty[j].getWriteMethod().invoke(target, sourceProperty[i].getReadMethod().invoke(source));
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyException(ResultEnum.ERROR.getCode(), "属性复制失败");
        }
    }
}
